﻿#region Đào Mạnh Hòa
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Transactions;

namespace BusinessLogic
{
    public static  class TheVip 
    {

        static SqlCommand cmd;
        static ArrayList arr;
        static SqlDataReader dr;
        static SqlConnection cn;

        #region select
        public static  Entities.TheVip[] Select(string ten)
        {
            Entities.TheVip[] list = null;
            try
            {
                string sql = Common.Constants.TheVip.Select;
                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(sql, cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("HanhDong", SqlDbType.VarChar, 50).Value = ten;
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.TheVip TheVip = new Entities.TheVip();
                    TheVip.TheVipID = int.Parse(dr[Common.Constants.TheVip.TheVipID].ToString());
                    TheVip.MaTheVip = dr[Common.Constants.TheVip.MaTheVip].ToString();
                    TheVip.MaKhachHang = dr[Common.Constants.TheVip.MaKhachHang].ToString();
                    TheVip.TenKhachHang = dr[Common.Constants.TheVip.TenKhachHang].ToString();
                    TheVip.LoaiThe = dr[Common.Constants.TheVip.LoaiThe].ToString();
                    TheVip.GiaTriConLai = (dr[Common.Constants.TheVip.GiaTriConLai].ToString());
                    TheVip.GhiChu = dr[Common.Constants.TheVip.GhiChu].ToString();
                    TheVip.Deleted = bool.Parse(dr[Common.Constants.TheVip.Deleted].ToString());
                    arr.Add(TheVip);
                }
                return (Entities.TheVip[])arr.ToArray(typeof(Entities.TheVip));
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); return new Entities.TheVip[0]; }
            finally
            {
                cmd.Connection.Dispose();
                cn.Close();
            }

        }
        #endregion

        #region insert
        public static  int InsertUpdate(Entities.TheVip TheVip)
        {
            TransactionScope ts =null;
            int rt = 0;
            try
            {
                string sql = Common.Constants.TheVip.InsertUpdate;
                cn = DataAccess.Connection.OpenConnection();
                using (ts = new TransactionScope(TransactionScopeOption.Required))
                {
                    SqlCommand cmd = new SqlCommand(sql, cn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(Common.Constants.TheVip.HanhDong, SqlDbType.NVarChar, 50).Value = TheVip.HanhDong;
                    cmd.Parameters.Add(Common.Constants.TheVip.TheVipID, SqlDbType.Int).Value = TheVip.TheVipID;
                    cmd.Parameters.Add(Common.Constants.TheVip.MaTheVip, SqlDbType.VarChar, 50).Value = TheVip.MaTheVip;
                    cmd.Parameters.Add(Common.Constants.TheVip.MaKhachHang, SqlDbType.VarChar, 50).Value = TheVip.MaKhachHang;
                    cmd.Parameters.Add(Common.Constants.TheVip.TenKhachHang, SqlDbType.NVarChar, 200).Value = TheVip.TenKhachHang;
                    cmd.Parameters.Add(Common.Constants.TheVip.LoaiThe, SqlDbType.NVarChar, 20).Value = TheVip.LoaiThe;
                    cmd.Parameters.Add(Common.Constants.TheVip.GiaTriConLai, SqlDbType.Float).Value = TheVip.GiaTriConLai;
                    cmd.Parameters.Add(Common.Constants.TheVip.GhiChu, SqlDbType.NVarChar, 200).Value = TheVip.GhiChu;
                    cmd.Parameters.Add(Common.Constants.TheVip.Deleted, SqlDbType.Bit).Value = TheVip.Deleted;
                    rt = cmd.ExecuteNonQuery();
                    if (TheVip.HanhDong == "Insert")
                    {
                        LuuVietHeThong.LuuViet(
                            "Thêm thẻ vip",
                            TheVip.TaiKhoan.MaNhanVien,
                            "Thêm thẻ vip mới " + TheVip.MaTheVip,
                            TheVip.TaiKhoan.TenDangNhap,
                            DateTime.Now.ToString("dd/MM/YYYY"));
                    }
                    ts.Complete();
                }
                cn.Close();
                return rt;
            }
            catch(SqlException ex)
            {
                ts.Dispose();
                cn.Close();
                return 0;
            }

        }
        #endregion
    }
}
#endregion